package com.nulstudio.sppm.controller;

import com.nulstudio.sppm.common.NulResult;
import com.nulstudio.sppm.entity.NulInvite;
import com.nulstudio.sppm.request.GenerateTokenRequest;
import com.nulstudio.sppm.service.InviteService;
import jakarta.annotation.Resource;
import org.springframework.lang.NonNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/invite")
public class InviteController {
    @Resource @NonNull
    private InviteService inviteService;

    @PreAuthorize("hasAuthority('admin')")
    @GetMapping
    @NonNull
    public NulResult<String> generate(@RequestBody @NonNull GenerateTokenRequest request) {
        final NulInvite invite = inviteService.generateInvite(request.expireTime(), request.role(),
                request.remaining(), request.blocked());
        return NulResult.response(invite.getCode());
    }

}
